package com.shcd.mapper;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface DataMonitoringMapper {

    //TODO 委内业务数据归集xxx万条
    Integer getWnDataSortCount(Map num);

    //TODO 空间数据编目
    int getSpaceCount();

    //TODO 业务数据编目
    int getBusinessCount();

    //TODO 业务数据编目_委内
    int getBusinessCount_wn();

    //TODO 业务数据编目_转发数据编目
    int getBusinessCount_zf();

    //TODO 建设管理,城市管理,房屋管理,综合管理
    Integer getWnDataSum(Map num);

    //TODO 数据质检总数
    int getQCSum();

    //TODO 数据清洗总数
    int getDataClearSum();

    //TODO 委数据资源中心总数
    int getDataResourceSum();

    //TODO 委档案中心总数
    int getRecordSum();

    //TODO 市大数据中心总数
    int getBigDataSum();

    //TODO 市信用中心总数
    int getCreditSum();

    //TODO 治理情况-质检
    Integer getGovernSituation(Map date);

    //TODO 治理情况-清洗
    Integer getClearSituation(Map date);

    //TODO 数据共享TOP10-业务
    @Select("select count,name from (select count,name from GXJH_APILOG_MIDDLE order by count desc) WHERE ROWNUM <= 10 order by count")
    List<Map> getDataShard_business();

    //TODO 数据共享TOP10-空间
    @Select("select count,name from (select count,name from T_SERVICE_USELOG_MIDDLE order by count desc) WHERE ROWNUM <= 10 order by count")
    List<Map> getDataShard_space();

    //TODO 共享情况-业务
    @Select("select count,month from gxjh_apilog_middle_shard order by month")
    List<Map> getShareSituation_business();

    //TODO 共享情况-空间
    @Select("select count,month from T_SERVICE_USELOG_MIDDLE_shard order by month")
    List<Map> getShareSituation_space();

    //TODO 调用情况-空间
    int getCellSituation_space();

    //TODO 调用情况-业务
    @Select("select count,sort from GXJH_APILOG_MIDDLE_CELL")
    List<Map> getCellSituation_business();

    //TODO 归集趋势
    @Select("select sum,month from LOG_EXCHANGE_MIDDLE_COLLECTS order by month")
    List<Map> getCollectsTrend();

    //TODO 查询主表
    @Select("<script>" +
            "select m.*, nvl(max(d.DataSource_Unit_Status),1) DataSource_Unit_Status " +
            "from PTJK_DataSourceStatus_Main m left join PTJK_DataSourceStatus_Detail d " +
            "on m.DATASOURCE_ID=d.DATASOURCE_ID " +
            "where 1=1 " +
            "<if test='name!=\"\"'> and DataSource_Name like '%${name}%' </if>" +
            "<if test='type!=\"\"'> and DataSource_TYPE = #{type} </if>" +
            "<if test='status!=\"\"'> and DataSource_Unit_Status = #{status} </if>" +
            "group by m.DATASOURCE_ID,m.DATASOURCE_NAME,m.DATASOURCE_IP,m.DATASOURCE_TYPE" +
            "</script>")
    List<Map> getChannelStatusMain(Map<String, String> m);

    //TODO 查询子表
    @Select("select d.* from PTJK_DataSourceStatus_Detail d where d.DataSource_Id = #{id}")
    List<Map> getChannelStatusDetail(Map<String, String> m);

    //TODO 插入主表
    @Insert("insert into PTJK_DataSourceStatus_Main(DataSource_Name,DataSource_IP,DataSource_TYPE) " +
            "values(#{name},#{ip},#{type})")
    Integer insertChannelStatusMain(Map<String, String> m);

    //TODO 根据ip查询主表id
    @Select("select DataSource_Id from PTJK_DataSourceStatus_Main where DataSource_IP = #{ip}")
    String getMainIdByIp (String m);

    //TODO 插入子表
    @Insert("insert into PTJK_DataSourceStatus_Detail(" +
            "DataSource_Id," +
            "DataSource_Unit_id," +
            "DataSource_Unit_Type," +
            "DataSource_Unit_Code," +
            "DataSource_Unit_Port," +
            "DataSource_Unit_Instance," +
            "DataSource_Unit_User," +
            "DataSource_Unit_PassWord," +
            "DataSource_Unit_Status) " +
            "values(#{id},#{unitId},#{unitType},#{unitCode},#{unitPort},#{unitInstance},#{unitUser},#{unitPassword},#{unitStatus})")
    Integer insertChannelStatusDetail(Map<String, String> m);

    @Delete("delete from PTJK_DataSourceStatus_Main where DataSource_Id = #{id}")
    Integer deleteChannelStatusMain(Map<String, String> m);

    @Delete("delete from PTJK_DataSourceStatus_Detail where DataSource_Id = #{id}")
    Integer deleteChannelStatusDetail(Map<String, String> m);

    @Update("update PTJK_DataSourceStatus_Main set DataSource_Name = #{name},DataSource_IP=#{ip},DataSource_TYPE=#{type} where DataSource_Id = #{id}")
    Integer updateChannelStatusMain(Map<String, String> m);

    @Select("<script>" +
            "select c.name, " +
            "       z.name unit_name, " +
            "       nvl(a.operating, '') operating, " +
            "       nvl(a.rid, '') rid, " +
            "       nvl(a.time, '') time, " +
            "       nvl(a.returninfo, '') returninfo, " +
            "       nvl(a.countnum, '') countnum, " +
            "       nvl(a.state, '') state, " +
            "       nvl(a.error_info, '') error_info, " +
            "       nvl(a.error_type, '') error_type, " +
            "       nvl(a.service_type, '') service_type, " +
            "       nvl(c.code, '') cid, " +
            "       nvl(a.count, 0) count " +
            "  from zjw_zybm_catalog c " +
            "  left join zyml_unit z " +
            "    on c.PROVIDERUNIT = z.code " +
            "  left join (select * " +
            "               from gxjh_apilog t1, " +
            "                    (select max(time) maxtime, " +
            "                            substr(cid, 1, 8) cid8, " +
            "                            count(*) count " +
            "                       from gxjh_apilog " +
            "                      group by cid) t2 " +
            "              where substr(t1.cid, 1, 8) = t2.cid8 " +
            "                and t1.time = t2.maxtime) a on a.cid8 = c.code " +
            " where c.states = 9 " +
            " and c.name like '%${name}%' " +
            "<if test='unitId!=\"\"'> and PROVIDERUNIT = #{unitId} </if>" +
            "<if test='startTime!=\"\"'> and time >= to_date('${startTime} 00:00:00', 'yyyy-mm-dd hh24:mi:ss') </if>" +
            "<if test='endTime!=\"\"'> and time &lt;= to_date('${endTime} 23:59:59', 'yyyy-mm-dd hh24:mi:ss') </if>" +
            " order by time desc nulls last" +
            "</script>")
    List<Map> getDataServiceStatesMonitoring(Map<String, String> m);

    @Select("<script>" +
            "select a.*,z.name unitName from " +
            " (select id," +
            "       operating, " +
            "       rid, " +
            "       time, " +
            "       returninfo, " +
            "       countnum, " +
            "       state, " +
            "       error_info, " +
            "       error_type, " +
            "       service_type, " +
            "       substr(cid, 1, 8) cid " +
            " from gxjh_apilog " +
            " where substr(cid, 1, 8) = #{cid} " +
            "<if test='state!=\"\"'> and state = #{state} </if>" +
            "<if test='startTime!=\"\"'> and time >= to_date('${startTime} 00:00:00', 'yyyy-mm-dd hh24:mi:ss') </if>" +
            "<if test='endTime!=\"\"'> and time &lt;= to_date('${endTime} 23:59:59', 'yyyy-mm-dd hh24:mi:ss') </if>" +
            " order by time desc) a " +
            " left join zjw_zybm_catalog c on c.code=a.cid " +
            " left join zyml_unit z on c.PROVIDERUNIT=z.code " +
            "</script>")
    List<Map> getDataServiceStatesMonitoring_Detail(Map<String, String> m);
}
